package com.ddyx.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddyx.entity.ActivityGoods;
import com.ddyx.entity.GoodsTag;
import com.ddyx.entity.GoodsTagContact;
import com.ddyx.mapper.GoodsMapper;
import com.ddyx.mapper.GoodsTagContactMapper;
import com.ddyx.mapper.GoodsTagMapper;
import com.ddyx.service.GoodsTagService;
import com.ddyx.util.MyBeanUtils;
import com.ddyx.util.RequestForm;
import com.ddyx.util.ResponseForm;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
@Slf4j
public class GoodsTagServiceImpl implements GoodsTagService {

	@Resource
	GoodsTagMapper gtMapper;
	
	@Resource
	GoodsTagContactMapper gtcMapper;

	
	@Resource
	GoodsMapper goodsMapper;
	@Override
	public ResponseForm getTagByStoreNo(String storeNo) {
		ResponseForm result = new ResponseForm();
		try {
			Example example = new Example(GoodsTag.class);
			example.setOrderByClause("tag_sort DESC");
			// example.setOrderByClause("DESC");
			Criteria criteria = example.createCriteria();

			criteria.andEqualTo("tagPid", 0);
			criteria.andEqualTo("storeNo", storeNo);
			criteria.andEqualTo("tagStatus", 0);
			List<GoodsTag> tList = gtMapper.selectByExample(example);
			if(tList.size()<1) {
				result.setStatus(true);
				result.setMessage("当前门店无自定义标签!");
				return result;
			}
			result.setData(tList);
			
			// categoryId为空时,默认查询最高层级分类,pid为0
			// if (tagId == null) {
			// // c.setCategoryPid(0);
			// criteria.andEqualTo("tagPid", 0);
			// List<GoodsTag> tList = gtMapper.selectByExample(example);
			// result.setData(tList);
			// return result;
			// }

			// 如果categoryId不为空,查询pid为当前categoryId的子分类
			// if (tagId != null) {
			// criteria.andEqualTo("tagPid", tagId);
			// List<GoodsTag> tList = gtMapper.selectByExample(example);
			// result.setData(tList);
			// if (tList.size() < 1) {
			// result.setMessage("当前分类无子分类");
			// }
			// }
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm addTag(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			Map paramMap = (Map) param.getData();
			GoodsTag tag = new GoodsTag();
			MyBeanUtils.populate(tag, paramMap);
			tag.setCreateTime(new Date());
			// if (tag.getTagPid() == null) {
			tag.setTagPid(0);
			// }
			
			List<String> goodsList = (List) paramMap.get("goodsList");
			if (!StringUtils.isNotBlank(tag.getTagPic())) {
				result.setMessage("自定义标签图片必须不能为空");
				result.setStatus(false);
				return result;
			}
			try {
//				Example example = new Example(GoodsTag.class);
//				example.setCountProperty("storeNo");
//				int num = gtMapper.selectCountByExample(example);
//				if (num > 9) {
//					result.setMessage("自定义标签已达上限");
//					result.setStatus(false);
//					return result;
//				}
				gtMapper.insertSelective(tag);
				if(goodsList!=null && goodsList.size()>0){
					GoodsTagContact gtc ;
					for (int i = 0; i < goodsList.size(); i++) {
						gtc=new GoodsTagContact();
						gtc.setTagId(tag.getTagId());
						Object goodsId= goodsList.get(i);
						gtc.setGoodsId((Integer)goodsId);
						gtcMapper.insertSelective(gtc);
					}
				}
				
				
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Transactional
	@Override
	public ResponseForm editTag(RequestForm param) {
		ResponseForm result = new ResponseForm();
		if (param.getData() != null) {
			Map paramMap = (Map) param.getData();
			GoodsTag tag = new GoodsTag();
			MyBeanUtils.populate(tag, paramMap);
			if (tag.getCreateTime() != null || tag.getStoreNo() != null || tag.getTagPid() != null) {
				result.setMessage("The filed is not able to edit!");
				result.setStatus(false);
			}
			List<String> goodsList = (List) paramMap.get("goodsList");
			try {
				gtMapper.updateByPrimaryKeySelective(tag);
				//删除所有该标签下的商品关系
				if(tag.getTagStatus()!=null) {
					GoodsTagContact gtc = new GoodsTagContact();
					gtc.setTagId(tag.getTagId());
					gtcMapper.delete(gtc);
					if(tag.getTagStatus()==0){
						if(goodsList!=null && goodsList.size()>0){
							GoodsTagContact gtc1 ;
							for (int i = 0; i < goodsList.size(); i++) {
								gtc1=new GoodsTagContact();
								gtc1.setTagId(tag.getTagId());
								Object goodsId= goodsList.get(i);
								gtc1.setGoodsId((Integer)goodsId);
								gtcMapper.insertSelective(gtc1);
							}
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		}
		return result;
	}

	@Override
	public ResponseForm getTagById(RequestForm param) {
		ResponseForm result = new ResponseForm();
			try {
				Map paramMap = (Map) param.getData();
				GoodsTag gt = new GoodsTag();
				MyBeanUtils.populate(gt, paramMap);
				gt = gtMapper.selectByPrimaryKey(gt);
			    List<Map<String, Object>> goodsList = goodsMapper.getTagGoodsList(gt.getTagId());
			    gt.setGoodsList(goodsList);
			    result.setData(gt);
			} catch (Exception e) {
				e.printStackTrace();
				result.setStatus(false);
				result.setMessage("操作有误");
				log.error("操作有误");
			}
		
		return result;
	}

	@Override
	public ResponseForm getTagByStoreNoPage(RequestForm param) {
		ResponseForm result = new ResponseForm();
		try {
			Map paramMap = (Map) param.getData();
			
			String storeNo=(String) paramMap.get("storeNo");
			if (!StringUtils.isNotBlank(storeNo)) {
				result.setMessage("门店no不能为空");
				result.setStatus(false);
				return result;
			}
			Example example = new Example(GoodsTag.class);
			example.setOrderByClause("tag_sort DESC");
			// example.setOrderByClause("DESC");
			Criteria criteria = example.createCriteria();

			criteria.andEqualTo("tagPid", 0);
			criteria.andEqualTo("storeNo", storeNo);
			criteria.andEqualTo("tagStatus", 0);
			com.ddyx.util.Page.parsePage(paramMap);
			Page<Object> page = PageHelper.startPage((Integer)paramMap.get("page"), (Integer)paramMap.get("size"));
			List<GoodsTag> tList = gtMapper.selectByExample(example);
			if(tList.size()<1) {
				result.setStatus(true);
				result.setMessage("当前门店无自定义标签!");
				return result;
			}
			result.setData(tList);
			result.setTotal((int) page.getTotal());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatus(false);
			result.setMessage("操作有误");
			log.error("操作有误");
		}
		return result;
	}

}
